<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Buyer_m extends MY_Model {

	public function __construct()
	{
		parent::__construct();
		$this->load->library('module_helper');
		//$this->load->library('music');
		//$this->load->library('country_city');
	}

	function list_activated(){
		$where = "status = 'active' AND acc_type='buyer' ";
		$total = count( $this->db->where($where)->get(TBL_ACCOUNT)->result() );
		$records_per_page = isset($_GET['records_per_page']) ? intval($_GET['records_per_page']) : 25;
		$per_page = isset($_GET['per_page']) ? intval($_GET['per_page']) : 0;
		
		$partial = array();
		foreach($_GET as $key=> $value){
			if($key != "per_page")
				$partial["$key"] = "$key=$value";
		}
		
		$pagination = create_pagination( 
								$uri = 'admin/dataman/buyer/activated/?'.implode('&',$partial), 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
	 	 
		$records = $this->db->where($where)->order_by('id','desc')->limit($records_per_page, $per_page )->get(TBL_ACCOUNT)->result();
		return array('pagination'=> $pagination, 'items'=>$records, 'total_records'=>$total);
	}
	
	function list_confirmed(){
		$where = "status = 'confirm' AND acc_type='buyer' ";
		$total = count( $this->db->where($where)->get(TBL_ACCOUNT)->result() );
		$records_per_page = isset($_GET['records_per_page']) ? intval($_GET['records_per_page']) : 25;
		$per_page = isset($_GET['per_page']) ? intval($_GET['per_page']) : 0;
		
		$partial = array();
		foreach($_GET as $key=> $value){
			if($key != "per_page")
				$partial["$key"] = "$key=$value";
		}
		
		$pagination = create_pagination( 
								$uri = 'admin/dataman/buyer/confirmed/?'.implode('&',$partial), 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
	 	 
		$records = $this->db->where($where)->order_by('id','desc')->limit($records_per_page, $per_page )->get(TBL_ACCOUNT)->result();
		return array('pagination'=> $pagination, 'items'=>$records, 'total_records'=>$total);
	}
	
	function list_pending(){
		$where = "status = 'pending' AND acc_type='buyer' ";
		$total = count( $this->db->where($where)->get(TBL_ACCOUNT)->result() );
		$records_per_page = isset($_GET['records_per_page']) ? intval($_GET['records_per_page']) : 25;
		$per_page = isset($_GET['per_page']) ? intval($_GET['per_page']) : 0;
		
		$partial = array();
		foreach($_GET as $key=> $value){
			if($key != "per_page")
				$partial["$key"] = "$key=$value";
		}
		
		$pagination = create_pagination( 
								$uri = 'admin/dataman/buyer/pending/?'.implode('&',$partial), 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
	 	 
		$records = $this->db->where($where)->order_by('id','desc')->limit($records_per_page, $per_page )->get(TBL_ACCOUNT)->result();
		return array('pagination'=> $pagination, 'items'=>$records, 'total_records'=>$total);
	}
	
	function search(){
		$s = ($_GET['key']);
		$filter = ($_GET['filter']);
		$options = array('acc_code'=>'Account Code','email'=>'Email','company'=>'Company', 'country'=>'Country');
		if(!in_array($filter, array_keys($options))){
			$filter = 'acc_code';
		}
		
		$records_per_page = isset($_GET['records_per_page']) ? intval($_GET['records_per_page']) : 25;
		$per_page = isset($_GET['per_page']) ? intval($_GET['per_page']) : 0;
		
		$total = count( $this->db->where(array('acc_type'=>'buyer',"$filter"=>$s))->get(TBL_ACCOUNT)->result() );
		
		$partial = array();
		foreach($_GET as $key=> $value){
			if($key != "per_page")
				$partial["$key"] = "$key=$value";
		}
		
		$pagination = create_pagination( 
								$uri = 'admin/dataman/buyer/search/?'.implode('&',$partial), 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
	 	 
		$records = $this->db->where(array('acc_type'=>'buyer',"$filter"=>$s))->order_by('id','desc')->limit($records_per_page, $per_page )->get(TBL_ACCOUNT)->result();
		
		return array('pagination'=> $pagination, 'items'=>$records, 'total_records'=>$total);
	}
	
	
	function activate(){
		$USER = $this->user_entity->init('id',$_POST['user_id']);
		$USER->setStatus('active');
		$USER->setCreatedDate(time());
		$USER->update();
		$this->session->set_flashdata('success', lang('dataman.success'));
		echo 'OK';
		exit();
	}
	
	function deactivate(){
		$USER = $this->user_entity->init('id',$_POST['user_id']);
		$USER->setStatus('pending');
		$USER->update();
		$this->session->set_flashdata('success', lang('dataman.success'));
		echo 'OK';
		exit();
	}
	
	function delete(){
		$USER = $this->user_entity->init('id',$_POST['user_id']);
		$this->db->where('account_code',$USER->getAccCode())->delete(TBL_BOOKING_FOR);
		$this->db->where('account_code',$USER->getAccCode())->delete(TBL_BID);
		$this->db->where('acc_code',$USER->getAccCode())->delete(TBL_ACCOUNT);
		$this->session->set_flashdata('success', lang('dataman.success'));
		echo 'OK';
		exit();
	}
	
	function submit_information(){
		$OTHER = clone_( $this->user_entity->init('email',$this->input->post('email','')) );
		
		if($OTHER AND $_POST['user_id'] != $OTHER->getId()){
			echo "This email is already existed in system. Please choose other email.";
			exit();
		}
		$USER = $this->user_entity->init('id',$_POST['user_id']);
		//get old data
		$revision = $USER->buildNewRevision();
		$company_profile = $USER->getCompanyProfile();
		if(isset($_FILES["company_profile"]) AND !empty($_FILES["company_profile"]['name'])){
			if(($_FILES["company_profile"]['size']/1000000) > allowMaxFileSize() ){
				$company_profile = '';
			}else{
				$company_profile = $this->module_helper->uploadFile( 
										$_FILES["company_profile"]['tmp_name'],
										$_FILES["company_profile"]['name'],
										$USER->getFolderFileStorage(),
										allowExtensionFileDocumentUpload()
									);
			}	
		}
		$array_init = array('show_type', 'country_origin', 'music_genre');
		foreach($array_init as $element){
			if(!isset($_POST[$element])){
				$_POST[$element] = array();
			}
		}
		$USER->setEmail( $this->input->post('email','') );
		$USER->setPassword( $this->input->post('password','') );
		$USER->setFirstName( $this->input->post('first_name','') );
		$USER->setLastName( $this->input->post('last_name','') );
		$USER->setJobTitle( $this->input->post('job_title','') );
		$USER->setCompany( $this->input->post('company','') );
		$USER->setCompanyUrl( $this->input->post('company_url','') );
		$USER->setCompanyProfile( $company_profile );
		$USER->setCountry( $this->input->post('country','') );
		$USER->setAddressLine1( $this->input->post('address_line_1','') );
		$USER->setAddressLine2( $this->input->post('address_line_2','') );
		$USER->setCity( $this->input->post('city','') );
		$USER->setState( $this->input->post('state','') );
		$USER->setZip( $this->input->post('zip','') );
		$USER->setWorkPhone( $this->input->post('work_phone','') );
		$USER->setMobile( $this->input->post('mobile','') );	
		$USER->setBankNameAcc( $this->input->post('bank_name_acc','') );
		$USER->setBankAccNo( $this->input->post('bank_acc_no','') );
		$USER->setBankName( $this->input->post('bank_name','') );
		$USER->setBankAddress( $this->input->post('bank_address','') );
		$USER->setSwiftCode( $this->input->post('swift_code','') );
		$USER->setShowType( implode(',',$_POST['show_type']) );
		$USER->setCountryOrigin( implode(',',$_POST['country_origin']) );
		$USER->setMusicGenre( implode(',',$_POST['music_genre']) );
		$USER->setBuyerType( $this->input->post('buyer_type','') );	
		$USER->setRevision($revision);
		$USER->update();
		
		$this->session->set_flashdata('success', lang('dataman.success'));
		echo 'OK';
		exit();
	}
	
	function submit_add_account(){
		if($this->user_entity->init('email',$this->input->post('email',''))){
			echo "This email is already existed in system. Please choose other email.";
			exit();
		}
		$getInsertId = $this->user_entity->createNewUserEntity($this->input->post('email',''));
		
		$USER = $this->user_entity->init('id',$getInsertId);
		$company_profile = "";
		if(isset($_FILES["company_profile"]) AND !empty($_FILES["company_profile"]['name'])){
			if(($_FILES["company_profile"]['size']/1000000) > allowMaxFileSize() ){
				$company_profile = '';
			}else{
				$company_profile = $this->module_helper->uploadFile( 
										$_FILES["company_profile"]['tmp_name'],
										$_FILES["company_profile"]['name'],
										$USER->getFolderFileStorage(),
										allowExtensionFileDocumentUpload()
									);
			}	
		}
		$USER->setAccCode( $this->digit->generateDigit4User( $this->input->post('type_acc','') ) );
		$array_init = array('show_type', 'country_origin', 'music_genre');
		foreach($array_init as $element){
			if(!isset($_POST[$element])){
				$_POST[$element] = array();
			}
		}
		$USER->setEmail( $this->input->post('email','') );
		$USER->setPassword( $this->input->post('password','') );
		$USER->setFirstName( $this->input->post('first_name','') );
		$USER->setLastName( $this->input->post('last_name','') );
		$USER->setJobTitle( $this->input->post('job_title','') );
		$USER->setCompany( $this->input->post('company','') );
		$USER->setCompanyUrl( $this->input->post('company_url','') );
		$USER->setCompanyProfile( $company_profile );
		$USER->setCountry( $this->input->post('country','') );
		$USER->setAddressLine1( $this->input->post('address_line_1','') );
		$USER->setAddressLine2( $this->input->post('address_line_2','') );
		$USER->setCity( $this->input->post('city','') );
		$USER->setState( $this->input->post('state','') );
		$USER->setZip( $this->input->post('zip','') );
		$USER->setWorkPhone( $this->input->post('work_phone','') );
		$USER->setMobile( $this->input->post('mobile','') );	
		$USER->setBankNameAcc( $this->input->post('bank_name_acc','') );
		$USER->setBankAccNo( $this->input->post('bank_acc_no','') );
		$USER->setBankName( $this->input->post('bank_name','') );
		$USER->setBankAddress( $this->input->post('bank_address','') );
		$USER->setSwiftCode( $this->input->post('swift_code','') );
		$USER->setShowType( implode(',',$_POST['show_type']) );
		$USER->setCountryOrigin( implode(',',$_POST['country_origin']) );
		$USER->setMusicGenre( implode(',',$_POST['music_genre']) );	
		$USER->setAccType( $this->input->post('type_acc','') );
		$USER->setStatus( 'active' );
		$USER->setCreatedDate( time() );
		$USER->setBuyerType( $this->input->post('buyer_type','') );	
		$USER->update();
		
		$this->session->set_flashdata('success', lang('dataman.success'));
		echo 'OK';
		exit();
	}
	
	
	
}
